// 导入db模块
const db = require('../config/db');

class Admin {
    // 管理员
    // 登录
    signin(admin_acc) {
        return new Promise((resolve, reject) => {
            // console.log('admin_acc:', admin_acc)
            let sql = `select admin_acc, admin_pwd,admin_id from db_admin where admin_acc = '${admin_acc}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            resolve({
                                code: 200,
                                msg: '登录成功。',
                                rows
                            })
                        }
                        else {
                            resolve({
                                code: -1,
                                msg: '账号不存在。',
                                rows
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }
    // 注册
    signup(admin_acc, admin_pwd, admin_id) {
        return new Promise((resolve, reject) => {
            let sql = `select admin_acc from db_admin where admin_acc = '${admin_acc}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length === 0) {
                            // 注册
                            let sql2 = `insert into db_admin(admin_acc,admin_pwd,admin_id) values('${admin_acc}','${admin_pwd}','${admin_id}')`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("x:",result)
                                        if (result.affectedRows === 1) {
                                            resolve({
                                                code: 200,
                                                msg: '注册账号成功。',
                                                admin_acc
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '注册账号失败。'
                                            })
                                        }

                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '注册账号失败。'
                                        })
                                    }
                                )
                        }
                        else {
                            // 账号已经存在
                            resolve({
                                code: 1,
                                msg: '该账号已经存在。',
                                admin_acc
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。',
                            err
                        })
                    }
                )
        })
    }
    
    // 管理用户模块
    // 删除账号
    remove_user(user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_acc from db_user where  user_id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_user where user_id = '${user_id}'`
                            db.query(sql2).then(
                                (result) => {
                                    if(result.affectedRows === 1){
                                        resolve({
                                            code: 200,
                                            msg: '删除账号成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除账号失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 用户列表
    list_user(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_user limit ${page_num}, ${page_size}`;
            let sql2 = `select count(*) as total from db_user`
            // resolve({page_num, page_size})
            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取用户列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取用户列表失败。',
                            err
                        })
                    }
                )
        })
    }

    // 搜索用户
    search(keyword) {
        return new Promise((resolve,reject)=>{
            let sql = `select * from db_user where user_acc like '%${keyword}%' or user_name like '%${keyword}%'`;
            db.query(sql)
            .then(
                (rows)=>{
                    resolve({
                        code: 200,
                        msg: `关键字“${keyword}”搜索的结果`,
                        result: rows 
                    })
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '关键字搜索失败。'
                    })
                },
            )
        })
    }

}
// 暴露Admin类实例
module.exports = new Admin();